
//
//  TotalDetailTableView.m
//  BOSS-SOOC
//
//  Created by apple on 16/9/5.
//  Copyright © 2016年 Gcx. All rights reserved.
//
#define Colorlu [UIColor colorWithRed:59.0/255 green:180.0/255 blue:121.0/255 alpha:1.0f]

#import "TotalDetailTableView.h"
#import "DianmingModel.h"
#import "DianMingTableViewCell.h"

@interface TotalDetailTableView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<DianmingModel *> *orders;

@end

@implementation TotalDetailTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    
    self = [super initWithFrame:frame style:style];
    
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = BGC;
        self.separatorColor = colordbdbdb;
        
        [self emptyWithTitle:@"暂无订单" image:[UIImage imageNamed:@"空白页-搜索无数据图片"]];

        [self registerClass:[DianMingTableViewCell class] forCellReuseIdentifier:@"DianMingTableViewCell"];

        [self setupRefresh];
        
        [self.mj_header beginRefreshing];
        
    }
    return self;
}

/**
 *  刷新加载
 */
- (void)setupRefresh{
    
    //    __block __weak OptionTableView *table = self;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self.mj_header beginRefreshing];
        
        [self isRefreshData:YES];
        
    }];
    header.stateLabel.textColor = COLOR_TITLE_999999;
    header.lastUpdatedTimeLabel.textColor = COLOR_TITLE_999999;
    self.mj_header = header;
    
    
    MJRefreshAutoNormalFooter * foot = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self.mj_footer beginRefreshing];
        
        [self isRefreshData:NO];
    }];
    foot.stateLabel.textColor = COLOR_TITLE_999999;
    self.mj_footer = foot;
    
}

#pragma mark -- DataSource

- (NSInteger)numberOfRowsInSection:(NSInteger)section{
    
    if (self.orders.count>0) {
        return 1;
    }else{
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.orders.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 83;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.001f;
    }
    
    return 10.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *ident = @"cell";
    
    DianMingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DianMingTableViewCell" owner:self options:nil][0];
    }
    
    DianmingModel *model = self.orders[indexPath.row];
    
    
    cell.titleLabel.font = NormalFont(42);
    cell.titleLabel.textColor = Color333333;
    
    cell.timeLabel.font = NormalFont(36);
    cell.timeLabel.textColor = Color999999;
    
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",model.ktzt];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@",model.skrq];
    cell.shidaoNum.text = [NSString stringWithFormat:@"%@",model.qdrs];
    cell.shidaoNum.textColor = Colorlu;
    cell.quexiNum.text = [NSString stringWithFormat:@"%@",model.qxrs];
    
    cell.shidaoLabel.textColor = Color333333;
    cell.shidaoLabel.font = NormalFont(39);
    
    cell.quexiLabel.textColor = Color333333;
    cell.quexiLabel.font = NormalFont(39);

    return cell;
}

#pragma mark - Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DianmingModel *model = self.orders[indexPath.row];
    
    if (self.sc_delegate && [self.sc_delegate respondsToSelector:@selector(tableView:didSelectModel:)]) {
        [self.sc_delegate tableView:self didSelectModel:model];
    }
}
- (void)isRefreshData:(BOOL)isRefresh{
    
    NSInteger pageIndex = 1;
    
    if (isRefresh) {
        
    } else {
        pageIndex = [self sc_currentPageNumWithTotalCount:self.orders.count];
    }
    
    NSMutableDictionary *parms = [[NSMutableDictionary alloc]init];
    [parms setObject:@"815bffb68e2ea44e38cf16a98a5a29a4" forKey:@"oauth_token"];
    [parms setObject:@"8e84bc64f03d73b5c75406ef1d7349f7" forKey:@"oauth_token_secret"];
    [parms setObject:@10 forKey:@"pageSize"];
    [parms setObject:[NSMutableString stringWithFormat:@"%ld",pageIndex] forKey:@"pageNo"];
    [parms setObject:self.kcid forKey:@"kcid"];
    
    SCHttpRequest *request = [[SCHttpRequest alloc]initWithApi:API_historyDetail params:parms];
    
    [self doPOSTConnect:request start:^(SCHttpRequest * _Nonnull req) {
        
    } success:^(SCHttpRequest * _Nonnull req, SCHttpResponse * _Nonnull resp) {
        
        
        NSLog(@"resp == %@", resp.originalDictionary);
        
        NSArray<DianmingModel*> *newArr = [DianmingModel mj_objectArrayWithKeyValuesArray:resp.array];
        
        if (isRefresh) {
            [self.orders removeAllObjects];
        }
        
        [self.orders addObjectsFromArray:newArr];
        
        [self reloadData];
        
        
    } failure:^(SCHttpRequest * _Nonnull req, NSError * _Nonnull error) {
        
    } finish:^(SCHttpRequest * _Nonnull req, BOOL success) {
        
        [self.mj_header endRefreshing];
        [self.mj_footer endRefreshing];
    }];
}
- (NSMutableArray<DianmingModel*>*)orders{
    
    if (!_orders) {
        _orders = [[NSMutableArray alloc]init];
    }
    return _orders;
}

- (NSInteger)sc_currentPageNumWithTotalCount:(NSInteger)total{
    
    NSInteger _page_size = 10;
    
    NSInteger pindex = total / _page_size;
    
    //如果有余数, 则在上方除10的时候把余数丢掉.下方进行判断, 如果有余数, 则+1处理
    if (total % _page_size) {
        pindex += 1;
    }
    return pindex + 1;
    
}
/**
 *  在没有内容的情况下获取内容
 */
- (void)loadDataOfNone{
    if (self.orders.count == 0) {
        [self.mj_header beginRefreshing];
    }
}

/**
 *  重置内部数据
 */
- (void)reset{
    
    [self.orders removeAllObjects];
    
    [self reloadData];
}


@end
